#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using System.Web;
using WaveMaster.Holidays.Club.Service;
using WaveMaster.Holidays.Club.Domain;
using System.Collections;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlLogin : UserControl
    {
        MainForm m_mainForm;
        TravelAgentService m_travelAgentService;
        UserService m_userService;
        User m_user;
        Keys m_currentKey;
        TravelAgent m_selectedTA;
        Timer m_searchTimer = new Timer();
        user_history m_userHistory = null;

        public UserControlLogin(MainForm mainForm)
        {
            InitializeComponent();
            m_mainForm = mainForm;
            m_travelAgentService = (TravelAgentService)ServiceFactory.Instance().GetService(ServiceFactory.TRAVEL_AGENT_SERVICE);
            m_userService = (UserService)ServiceFactory.Instance().GetService(ServiceFactory.USERSERVICE);
            textBoxMessage.Visible = false;

            m_searchTimer.Interval = 100;
            m_searchTimer.Tick += new EventHandler(m_searchTimer_Tick);
           // comboBoxTravelAgent.DataSource = m_travelAgentService.LoadAllActiveTravelAgent();
            //comboBoxTravelAgent.Text = "";

            //searchTravelAgentComboBox2.Enter += new EventHandler(searchTravelAgentComboBox2_Enter);
            //m_user = new User();
            //m_user.ActiveCode = true;
            //m_user.EmailID = "dagado@gmail.com";
            //m_user.ExpiryDate = DateTime.Today.AddYears(1);
            //m_user.ModificationTerminal = HttpContext.Current.User.Identity.Name.ToString() + "," + HttpContext.Current.Request.UserHostAddress;
            //m_user.Password = "admin123";
            //m_user.UserCode = "administrator";
            //m_user.UserName = "Administrator";
            //m_userService.InsertUser(m_user);
        }

       

        void searchTravelAgentComboBox2_Enter(object sender, EventArgs e)
        {
            //searchTravelAgentComboBox2. 
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            if (!isvalidForm())
            { return; }
            if(isValidUser())
            {
                this.Hide();
               // m_user.Role = m_userService.GetRoleMasterByID(m_user.Role.RoleMasterID);
                m_userService.GetUserRoleMasterByUserID(m_user);
                m_user.TravelAgent = m_travelAgentService.GetTravelAgentByID(m_user.TravelAgent.TravelAgentID);
                m_mainForm.User = m_user;
                insertloginSession();
                m_mainForm.ShowMainFormPanel(true);
                textBoxMessage.Visible = false;
            }
            //else
            //{
            //    SetMessage("The user name or password you entered isn't correct. Try entering it again.");
            //}
            clearForm();
            textBoxUserCode.SelectAll();
        }

        private void insertloginSession()
        {
            m_userHistory = m_userService.AddUserHistory(m_user, this.Session.SessionID, "");
        }

        public void InsertlogoutSession()
        {
           // m_userService.UpdateLogOut(m_user);
           // comboBoxTravelAgent.DataSource = m_travelAgentService.LoadAllActiveTravelAgent();
            //comboBoxTravelAgent.Text = "";
            if(m_userHistory!=null)
                m_userService.UpdateEndDate(m_userHistory);
            textBoxTravelAgent.Text = "";
            textBoxUserCode.Text = "";
            textBoxPassword.Text = "";
            HttpContext.Current.Session.Abandon();
            HttpContext.Current.Session.Clear();
        }

        private void clearForm()
        {
            textBoxPassword.Text = "";
            textBoxUserCode.SelectAll();
        }

        public void SetMessage(string text)
        {
            textBoxMessage.Text = text;
            textBoxMessage.Visible = true;
        }

        private bool isValidUser()
        {
            //TravelAgent ta = (TravelAgent)comboBoxTravelAgent.SelectedValue;
            TravelAgent ta = m_selectedTA;
            m_user = m_userService.GetUserByUserCodeAndAgentID(textBoxUserCode.Text, ta.TravelAgentID);
            if (m_user == null)
            {
                SetMessage("The user code or password you entered isn't correct. Try entering it again.");
                return false;
            }
            if (!m_user.ActiveCode)
            {
                SetMessage("The user code you entered is inactive.");
                return false;
            }
            if (m_user.PasswordExpired)
            {
                if (m_user.ExpiryDate.Date < DateTime.Today)
                {
                    SetMessage("The user code you entered is expired.");
                    return false;
                }
            }
            if (textBoxPassword.Text != m_user.Password)
            {
                SetMessage("The user code or password you entered isn't correct. Try entering it again.");
                return false;
            }
            return textBoxPassword.Text == m_user.Password;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            textBoxUserCode.Focus();
        }

       

        private void textBoxUserCode_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (objArgs.KeyData == Keys.Enter)
            {
                textBoxPassword.SelectAll();
            }
        }

        private void textBoxPassword_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_currentKey = objArgs.KeyData;
            if (objArgs.KeyData == Keys.Enter)
            {
                textBoxTravelAgent.Focus();
            }

        }

        private void textBoxPassword_TextChanged(object sender, EventArgs e)
        {
            //if (m_currentKey == Keys.Enter)
            //{
            //    //comboBoxTravelAgent.Focus();
            //    this.SelectNextControl(textBoxPassword, true, true, true,true);
            //}
        }
        private void comboBoxTravelAgent_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            if (m_currentKey == Keys.Enter)
            {
                buttonLogin_Click(null, null);
            }
        }


        public bool isvalidForm()
        {
            //bool valid = comboBoxTravelAgent.SelectedValue != null;
            //if (!valid)
            //{
            //    SetMessage("Please choose Travel Agent.");
            //}
            bool valid = m_selectedTA != null;
            if (!valid)
            {
                SetMessage("Please choose Travel Agent.");
            }
            return valid;
        }

        private void textBoxUserCode_GotFocus(object sender, EventArgs e)
        {
            textBoxUserCode.SelectAll();
        }

        private void textBoxPassword_GotFocus(object sender, EventArgs e)
        {
            textBoxPassword.SelectAll();
        }

        private void buttonSearchTA_Click(object sender, EventArgs e)
        {

        }



        internal void LoadBook(TravelAgent ta)
        {
            textBoxTravelAgent.Text = ta.Name;
            m_selectedTA = ta;
        }

        private void buttonSearchTA_Click_1(object sender, EventArgs e)
        {
            IList travelagents = m_travelAgentService.SearchActiveTravelAgentByCodeName(textBoxTravelAgent.Text);
            if (travelagents.Count == 1)
            {
                m_selectedTA = (TravelAgent)travelagents[0];
                textBoxTravelAgent.Text = m_selectedTA.Name;
                buttonLogin.Focus();
            }
            else
            {
                SearchTravelAgentForm sta = new SearchTravelAgentForm(this, textBoxTravelAgent.Text);
                sta.ShowDialog();
            }
        }

        private void textBoxTravelAgent_GotFocus(object sender, EventArgs e)
        {
            textBoxTravelAgent.SelectAll();
        }

        private void textBoxTravelAgent_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                m_searchTimer.Start();
            }
        }
         void m_searchTimer_Tick(object sender, EventArgs e)
        {
            buttonSearchTA_Click_1(sender, e);
            m_searchTimer.Stop();

        }
    }
}